package com.ereport.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.ereport.entity.Accounts;
import com.ereport.entity.Expenses;
import com.ereport.util.ApplicationContext;

public class ConveyanceExpDAO {
static Transaction tx;
	
	private  void endOperations(Session session) {
		
		session.close();
		session=null;
	}

	private  Session startOperations()throws Exception {
		
		Session session = ApplicationContext.getHiberSession().openSession();
		tx=session.beginTransaction();
		return session;
		
	}
	public int insert(Accounts ac) throws Exception {
		Session session = startOperations();
		session.saveOrUpdate(ac);
		session.getTransaction().commit();
		endOperations(session);
		return 0;
		
	}

	public List<Expenses> getExpRep(int id, String mnth, String yr) throws Exception {
		List<Expenses> ob=new ArrayList<Expenses>();
		Session session = startOperations();
		String qrystr="";
		if(mnth==null || mnth==""){
			qrystr="From Expenses where exe_id=:id and expdate like '"+yr+"%'";
		}else {
			qrystr="From Expenses where exe_id=:id and expdate like '"+yr+"-"+mnth+"%'";
		}
		Query qr=session.createQuery(qrystr);
		qr.setInteger("id", id);
		ob=qr.list();
		
		session.getTransaction().commit();
		endOperations(session);
		return ob;
		
	}
}
